package com.yiren.manager.ctrl.desk;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrInvoiceApply;
import com.yiren.entity.YrStaffInfo;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.desk.YrInvoiceApplyForm;
import com.yiren.manager.service.iface.department.YrStaffInfoService;
import com.yiren.manager.service.iface.desk.YrInvoiceApplyService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("invoiceApply")
@Controller
public class YrInvoiceApplyController extends GenericController<YrInvoiceApplyForm, YrInvoiceApply>{
	
	@Autowired
	private YrInvoiceApplyService selfService;
	
	@Autowired
	private YrStaffInfoService staffInfoService;
	
	//保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrInvoiceApplyForm> save(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		HTTPResult<YrInvoiceApplyForm> result = new HTTPResult<YrInvoiceApplyForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrInvoiceApplyForm form = tool.mapper("invoiceApply", YrInvoiceApplyForm.class);
		if(null==form){
			throwExp("param.error");
		}
		Date date = new Date();
		if(null==form.getInvoiceApplyId()||form.getInvoiceApplyId().isEmpty()){
			form.setInvoiceApplyId(UUIDGenerator.getUUID());
			form.setCreateTime(date);
		}
		form.setUpdateTime(date);
		form.setApplyTime(date);
		form.setApplyId(form.getCreateId()==null?"-1":form.getCreateId());
		form.setApproveResult(0);
		form.setGetStatus(0);
		if(null!=form.getCreateId()){
			YrStaffInfo staffInfo = staffInfoService.findOne(form.getCreateId());
			form.setApplyName(staffInfo==null?"":staffInfo.getName());
		}

		YrInvoiceApply entity = ObjectUtils.objectToObject(form, YrInvoiceApply.class);
		selfService.save(entity);
		
		return result;
	}
	
	/**
	 * 查询某个月份的记录
	 * @param month
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("findByMonth")
	@ResponseBody
	public HTTPResult<YrInvoiceApplyForm> findByMonth(String month) throws Exception{
		HTTPResult<YrInvoiceApplyForm> result = new HTTPResult<YrInvoiceApplyForm>();
		String sqlWhere = "DATE_FORMAT(apply_time,'%Y-%m') = ?1";
		QueryResult<YrInvoiceApply> qr = selfService.find(YrInvoiceApply.class, sqlWhere, new Object[]{month});
		
		List<YrInvoiceApplyForm> formList = new ArrayList<YrInvoiceApplyForm>();
		for(YrInvoiceApply entity : qr.getResultList()){
			YrInvoiceApplyForm form = toForm(entity);
			formList.add(form);
		}
		
		result.setData(formList);
		
		return result;
	}
	
	@RequestMapping("findAll")
	@ResponseBody
	public HTTPResult<YrInvoiceApplyForm> findAll() throws Exception{
		HTTPResult<YrInvoiceApplyForm> result = new HTTPResult<YrInvoiceApplyForm>();
		
		String sqlWhere = "get_status=0 AND approve_result=0";
		QueryResult<YrInvoiceApply> qr = selfService.find(YrInvoiceApply.class, sqlWhere, new Object[]{});
		
		List<YrInvoiceApplyForm> formList = new ArrayList<YrInvoiceApplyForm>();
		for(YrInvoiceApply entity : qr.getResultList()){
			YrInvoiceApplyForm form = toForm(entity);
			formList.add(form);
		}
		
		result.setData(formList);
		return result;
	}

	@Override
	public YrInvoiceApplyForm toForm(YrInvoiceApply e) throws Exception {
		YrInvoiceApplyForm form = ObjectUtils.objectToObject(e, YrInvoiceApplyForm.class);
		return form;
	}

	@Override
	public YrInvoiceApply toEntity(YrInvoiceApplyForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
